package com.cn.longdistancebusstation.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.longdistancebusstation.MainActivity;
import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.annotation.LoginStatus;
import com.cn.longdistancebusstation.base.BaseFragment;
import com.cn.longdistancebusstation.callback.OnCustomViewClickListener;
import com.cn.longdistancebusstation.global.GlobalVariable;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Integration;
import com.cn.longdistancebusstation.model.Result;
import com.cn.longdistancebusstation.personal.AboutActivity;
import com.cn.longdistancebusstation.personal.AlterPasswordActivity;
import com.cn.longdistancebusstation.personal.AlterTelephoneNumberActivity;
import com.cn.longdistancebusstation.personal.CouponsActivity;
import com.cn.longdistancebusstation.personal.HelpActivity;
import com.cn.longdistancebusstation.contacter.ContacterListActivity;
import com.cn.longdistancebusstation.personal.privilege.PrivilegeListActivity;
import com.cn.longdistancebusstation.scenic.order.ScenicOrderListActivity;
import com.cn.longdistancebusstation.ui.MyOrderActivity;
import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2018/1/15.
 */

public class UserFragment extends BaseFragment implements View.OnClickListener{
    private TextView musernameTextView,mIntegrationTextView;
    private RelativeLayout order,scenicOrder,passenger,coupons,select_pwd,help,select_teiephone_number,about_us;
    private Button btn_logout;
    private OnCustomViewClickListener mOnCustomViewClickListener;
    private String mIntegertionString;
    private SharedPreferences mSharedPreferences;
    private String strUserName;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.view_user,null);
        musernameTextView=(TextView)view.findViewById(R.id.username);
        mIntegrationTextView=(TextView)view.findViewById(R.id.integration_details);
        order=(RelativeLayout)view.findViewById(R.id.myorder);
        order.setOnClickListener(this);
        scenicOrder=(RelativeLayout)view.findViewById(R.id.scenic_order);
        scenicOrder.setOnClickListener(this);
        passenger=(RelativeLayout)view.findViewById(R.id.passenger);
        passenger.setOnClickListener(this);
        coupons=(RelativeLayout)view.findViewById(R.id.coupons);
        coupons.setOnClickListener(this);
        select_pwd=(RelativeLayout)view.findViewById(R.id.select_pwd);
        select_pwd.setOnClickListener(this);
        help=(RelativeLayout)view.findViewById(R.id.help);
        help.setOnClickListener(this);
        select_teiephone_number=(RelativeLayout)view.findViewById(R.id.select_telephone_number);
        select_teiephone_number.setOnClickListener(this);
        about_us=(RelativeLayout)view.findViewById(R.id.about);
        about_us.setOnClickListener(this);
        btn_logout=(Button)view.findViewById(R.id.btn_exit);
        btn_logout.setOnClickListener(this);
        return view;

    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.myorder:
                intent=new Intent(getActivity(), MyOrderActivity.class);
                startActivity(intent);
                break;
            case R.id.scenic_order:
                intent=new Intent(getActivity(), ScenicOrderListActivity.class);
                startActivity(intent);
                break;
            case R.id.passenger:
                intent = new Intent(getActivity(), ContacterListActivity.class);
                startActivity(intent);
                break;
            case R.id.coupons:
//                Intent in2=new Intent(getActivity(), CouponsActivity.class);
                intent = new Intent(getActivity(), PrivilegeListActivity.class);
                startActivity(intent);
                break;
            case R.id.select_pwd:
                intent = new Intent(getActivity(), AlterPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.help:
                helpMessage();
                break;
            case R.id.select_telephone_number:
                select_telephoneNo();
                break;
            case R.id.about:
                intent = new Intent(getActivity(),AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_exit:
                logout();
                break;
        }

    }
    private void select_telephoneNo(){
        Intent intent=new Intent(getActivity(), AlterTelephoneNumberActivity.class);
        startActivity(intent);
    }

    private void helpMessage() {
        Intent in=new Intent(getActivity(), HelpActivity.class);
        startActivity(in);
    }
    private void logout(){
        GlobalVariable.setLoginStatus(LoginStatus.STATUS_LOGOUT);
        SharedPreferences.Editor editor=getActivity().getSharedPreferences("data",Context.MODE_PRIVATE).edit();
        String username=" ";
        editor.putString("telephone",username);
        editor.apply();
        mOnCustomViewClickListener.onClick();
    }

    public OnCustomViewClickListener getOnCustomViewClickListener() {
        return mOnCustomViewClickListener;
    }

    public void setOnCustomViewClickListener(OnCustomViewClickListener onCustomViewClickListener) {
        mOnCustomViewClickListener = onCustomViewClickListener;
    }
    private void getIntegrations() {
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService httpService=retrofit.create(HttpService.class);
        Call<Result<Integration>> call=httpService.getIntegration(GlobalVariable.getUserID());
        showHud();
        call.enqueue(new Callback<Result<Integration>>() {
            @Override
            public void onResponse(Call<Result<Integration>> call, Response<Result<Integration>> response) {
                hideHud();
                Gson gson = new Gson();
                Result result = response.body();
                Boolean isSuccess = result.getHead().getSuccess();
                Integration integration=(Integration) result.getBody();
                if(isSuccess){
                    String integrations=integration.getIntegral();
//                    mIntegrationTextView.setText("出行积分：" + integrations);
                    mIntegrationTextView.setText(integrations);
                }
            }

            @Override
            public void onFailure(Call<Result<Integration>> call, Throwable t) {
                hideHud();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
        String uname=sharedPreferences.getString("telephone","");
        musernameTextView.setText(uname);
        getIntegrations();
        mSharedPreferences=getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
        strUserName=mSharedPreferences.getString("telephone","");
        mIntegertionString=mSharedPreferences.getString("integration","");
        musernameTextView.setText(strUserName);

    }
}
